//
//  AssignToJsonModel.m
//  ArtPage
//
//  Created by 张旭 on 16/9/8.
//  Copyright © 2016年 zhangxu. All rights reserved.
//

#import "AssignToJsonModel.h"

@implementation AssignToJsonModel

+(NSMutableArray *)Model:(NSString *)modelClass ToArray:(id)jsonArray
{
    unsigned int proCount = 0;
    
    objc_property_t *properties = class_copyPropertyList(NSClassFromString(modelClass), &proCount);
    NSMutableArray *mutableArray = [[NSMutableArray alloc]initWithCapacity:1];
    for (int i = 0; i < [jsonArray count]; ++i)
    {
        id dic = jsonArray[i];
        if (! [dic isKindOfClass:[NSDictionary class]])
        {
            continue;
        }
//        model是一个类的对象
        id model = [[NSClassFromString(modelClass) alloc]init];
        
        for (int j = 0; j < proCount; ++j)
        {
            objc_property_t property = properties[j];
            NSString *propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
            id value = [dic objectForKey:propertyName ];
            if (!value || [value isKindOfClass:[NSNull class]])
            {
                continue;
            }
            [model setValue:value forKey:propertyName];
        }
        [mutableArray addObject:model];
    }
    return mutableArray;
}
@end
